import pandas as pd
import csv

def clean_data(data, sensitive_words):
    """
    数据清洗：去除空值、过滤敏感词
    data: 待清洗的数据
    sensitive_words: 敏感词列表
    返回清洗后的数据
    """
    cleaned_data = []
    for news in data:
        # 去除空值
        if all(news.values()):
            # 过滤敏感词
            has_sensitive_word = False
            for key in news:
                if isinstance(news[key], str) and any(word in news[key] for word in sensitive_words):
                    has_sensitive_word = True
                    break
            if not has_sensitive_word:
                cleaned_data.append(news)
    return cleaned_data

def save_to_csv(data, file_path):
    """
    将新闻数据保存为CSV文件
    data: 待保存的数据
    file_path: CSV文件路径
    """
    if not data:
        print("没有有效数据可供保存。")
        return
    fieldnames = data[0].keys()
    try:
        with open(file_path, mode='w', newline='', encoding='utf-8-sig') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            # 写入表头
            writer.writeheader()
            # 写入数据
            for news in data:
                writer.writerow(news)
    except Exception as e:
        print(f"保存CSV文件时出错: {e}")